#include "StdAfx.h"
#include "BoardItems.h"
#include <boost/mpl/bool.hpp>


namespace catan
{
	BoardItems::BoardItems(void)
	{
		mCreateId = 0;
		
		//List::head_type
		//boost::get<Board*>(mList);
		//mList.get<Board*>();
	}
	
	BoardItems::~BoardItems(void)
	{

	}

	void BoardItems::Clear()
	{
		for (Datas::iterator it(mDatas.begin());
			it != mDatas.end(); ++it)
		{
			if(*it)
			{
				delete *it;
				*it = 0;
			}
		}
		mDatas.clear();
	}

	class RemapArchive 
	{
	public:
		RemapArchive(BoardItems* boarditems)
			: mBoardItems(boarditems)
		{

		}
		friend class boost::archive::save_access;

		// member template for saving primitive types.
		// Specialize for any types/templates that special treatment
		template<class T>
		void save(T & t)
		{

		}

	public:
		//////////////////////////////////////////////////////////
		// public interface used by programs that use the
		// serialization library

		// archives are expected to support this function
		void save_binary(void *address, std::size_t count)
		{

		}
// 
// 		//////////////////////////////////////////////////////////
// 		// public interface used by programs that use the
// 		// serialization library
// 		typedef boost::mpl::bool_<true> is_saving; 
// 		typedef boost::mpl::bool_<false> is_loading;
// 		template<class T> void register_type(){}
		template <class T> void Save(T & t)
		{
			
		}

		template <class HandleType>
		void Save(Handle<HandleType>& h)
		{
			if (h.Id() && !h)
			{
				h = Handle<HandleType>(h.Id(), mBoardItems->Get<HandleType>(h.Id()));
			}
			
		}

		template <class HandleType>
		void Save(Buildings<HandleType>& buildings)
		{
			*this & buildings.mTys;
		}

		template <class HandleType>
		void Save(std::vector<Handle<HandleType>>& vect)
		{
			for (typename std::vector<Handle<HandleType>>::iterator it(vect.begin());
				it != vect.end(); ++it)
			{
				*this & *it;
			}
		}

		template <class HandleType>
		void Save(std::list<Handle<HandleType>>& list)
		{
			for (typename std::list<Handle<HandleType>>::iterator it(list.begin());
				it != list.end(); ++it)
			{
				*this & *it;
			}
		}

		template <class First, class Second>
		void Save(std::map<First, Second*>& maps)
		{
			for (typename std::map<First, Second*>::iterator it(maps.begin());
				it != maps.end(); ++it)
			{
				it->second->serialize(*this, 1);
			}
		}
		template <> void Save<BoardItems>(BoardItems& b)
		{
			b.serialize(*this, 1);
		}

 		template<class T> RemapArchive & operator<<(T & t)
		{
			Save(t);
 			return *this;
 		}

 		template<class T> RemapArchive & operator&(T & t){
 			return *this << t;
 		}
// 		void save_binary(void *address, std::size_t count){};
		BoardItems* mBoardItems;

	};


	void BoardItems::Remap()
	{
		tstringstream oss;
		RemapArchive os(this);
		//RemapArchive a(this);
		os & *this;
		//a & this;
	}
}	
